package com.zh.yqbj.controller;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.yqbj.entity.Note;
import com.zh.yqbj.entity.Space;
import com.zh.yqbj.entity.SpaceNote;
import com.zh.yqbj.entity.UserSpace;
import com.zh.yqbj.service.NoteService;
import com.zh.yqbj.service.SpaceNoteService;
import com.zh.yqbj.service.SpaceService;
import com.zh.yqbj.service.UserSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private SpaceService spaceService;
    @Autowired
    private UserSpaceService userSpaceService;
    @Autowired
    private SpaceNoteService spaceNoteService;
    @Autowired
    private NoteService noteService;

    @GetMapping
    public SaResult treeTest() {
        // 构建node列表
        List<String> userSpaceIds =
                userSpaceService.list(new LambdaQueryWrapper<UserSpace>().eq(UserSpace::getUserId,
                        "549881267295758092")).stream().map(UserSpace::getSpaceId).collect(Collectors.toList());
        List<Space> spaceList = spaceService.list(new LambdaQueryWrapper<Space>().in(Space::getId, userSpaceIds));
        for (Space space : spaceList) {
            log.info("space{}", space.toString());
        }
        //2.配置
        TreeNodeConfig config = new TreeNodeConfig();
        config.setIdKey("id");
        config.setNameKey("spaceName");
        config.setParentIdKey("parentId");
        config.setDeep(3);
        config.setChildrenKey("children");
        List<Tree<String>> treeList = TreeUtil.build(spaceList, "0", config,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getSpaceName());
                    // 扩展属性 ...
                    tree.putExtra("key", treeNode.getId());
                    tree.putExtra("spaceSize", treeNode.getSpaceSize());
                    tree.putExtra("type", treeNode.getType());
                    // 查出该空间下的笔记
                    List<String> noteIds =
                            spaceNoteService.list(new LambdaQueryWrapper<SpaceNote>().eq(SpaceNote::getSpaceId,
                                            treeNode.getId()))
                                    .stream().map(SpaceNote::getNoteId).collect(Collectors.toList());
                    List<Note> noteList = new ArrayList<>();
                    //mybatis-plus未做处理，只能手动判空
                    if (CollUtil.isNotEmpty(noteIds)) {
                        noteList = noteService.listByIds(noteIds);
                    }
                    tree.putExtra("noteList", noteList
                    );
                });
        return SaResult.data(treeList);
    }
}
